<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Request;
use think\Session;

class Createdata extends  Controller
{
    public $ps_storeinfo = array();

    public function _initialize()
    {
        header('Content-Type:text/html; charset=utf-8');
        //判断用户是否登录
        //判断权限
        //登录有效期
    }
    public function __construct(){
        $this->ps_storeinfo = Db::name('ps_storeinfo')->field('storeName')->select();
    }
    //返回最近一次下单时间和最近一次付款时间
    public function return_need_time($condition){
        if ($condition=='order_time'){
            $arr = array(7,8,8,8,8,8,8);
            $nian = $arr[rand(0,count($arr)-1)];
            if ($nian==7) {
                $yue = rand(11,12);
            }elseif ($nian==8){
                $yue = rand(1,3);
            }
            if ($yue<10) {
                $yue = '0'.$yue;
            }
            if($yue=='01' || $yue=='02'){
                $ri = rand(1,28);
            }elseif($yue=='11' || $yue=='12'){
                $ri = rand(1,30);
            }elseif($yue=='03'){
                $ri = rand(1,20);
            }
            if ($ri<10) {
                $ri = '0'.$ri;
            }
            $shi = rand(7,23);
            $fen = rand(1,55);
            $miao = rand(1,55);
            $miao1 = $miao*1+rand(1,3)*1;
            if ($shi<10){
                $shi = '0'.$shi;
            }
            if ($fen<10){
                $fen = '0'.$fen;
            }
            if ($miao<10){
                $miao = '0'.$miao;
            }
            $date['order_time'] = '201'.$nian.'-'.$yue.'-'.$ri.' '.$shi.':'.$fen.':'.$miao;
            $date['pay_time'] = '201'.$nian.'-'.$yue.'-'.$ri.' '.$shi.':'.$fen.':'.$miao1;
            $date = json_encode($date);
        }
        elseif($condition=='zhuce_time'){
            $arr = array(6,7,7,7,8,8);
            $nian = $arr[rand(0,count($arr)-1)];
            if ($nian==6) {
                $yue = rand(9,12);
            }elseif ($nian==7){
                $yue = rand(1,12);
            }elseif ($nian==8){
                $yue = rand(1,3);
            }
            if ($yue<10) {
                $yue = '0'.$yue;
            }
            if ($nian==8 && $yue=='03'){
                $ri = rand(1,17);
            }else{
                if ($yue=='02'){
                    $ri = rand(1,28);
                }elseif($yue=='01' || $yue=='03' || $yue=='05' || $yue=='07' || $yue=='08' || $yue=='10' || $yue=='12'){
                    $ri = rand(1,31);
                }elseif($yue=='04' || $yue=='06' || $yue=='09' || $yue=='11'){
                    $ri = rand(1,30);
                }
            }
            if ($ri<10) {
                $ri = '0'.$ri;
            }
            $shi = rand(7,23);
            $fen = rand(1,55);
            $miao = rand(1,55);
            if ($shi<10){
                $shi = '0'.$shi;
            }
            if ($fen<10){
                $fen = '0'.$fen;
            }
            if ($miao<10){
                $miao = '0'.$miao;
            }
            $date['zhuce_time'] = '201'.$nian.'-'.$yue.'-'.$ri.' '.$shi.':'.$fen.':'.$miao;
            $date = json_encode($date);
        }
        return $date;
    }
    public function one_data(){
        for($i=0;$i<=50;$i++){
            $zhuce_time_arr = json_decode($this->return_need_time('zhuce_time'),true);
            $ordr_time_arr = json_decode($this->return_need_time('order_time'),true);
            if ($zhuce_time_arr['zhuce_time']<$ordr_time_arr['order_time']){
                break;
            }
        }
//        $res = Db::name('ps_storeinfo')->field('storeName')->select();
        $data['zhuce_time'] = $zhuce_time_arr['zhuce_time'];
        $data['order_time'] = $ordr_time_arr['order_time'];
        $data['pay_time'] = $ordr_time_arr['pay_time'];
        $data['storename'] = $this->ps_storeinfo[rand(1,6578)]['storeName'];
        if (time()%2==0){
            $amount = rand(1,250);
        }else{
            $num =  1 + mt_rand() / mt_getrandmax() * (250 - 1);
            if (rand(1,10)%2==0){
                $amount = sprintf("%.2f", $num);
            }else{
                $amount = sprintf("%.1f", $num);
            }
        }
        $data['amount'] = $amount;
        $phone = [134,135,136,137,138,139,147,150,151,152,157,158,159,182,187,188,130,131,132,155,156,185,186,133,153,180,189];
        $data['member'] = $phone[rand(0,26)].rand(10000000,99999999);
        $data['phone'] = $data['member'];
        return json_encode($data);
    }
    public function insert_db(){
        for($i=1;$i<=500;$i++){
            $list[$i] = json_decode($this->one_data(),true);
        }
        $res = Db::table('member')->insertAll($list);
        return $res;
    }
    public function start_create(){
        $stime=microtime(true);

        for($i=1;$i<=200;$i++){
            echo $i;
            $this->insert_db();
            sleep(1);
        }

        $etime=microtime(true);//获取程序执行结束的时间
        $total=$etime-$stime;  //计算差值
        var_dump($total);
    }

    public function aaa(){
        $res = Db::name('member')->where("id>=2593300 and id<=2593310")->select();
        var_dump($res);
    }

}
